jQuery(document).on('click', '.edit_lead', function() {

    jQuery(document).ready(() => {
        jQuery("#amt_val").val('Select Insurance');
    });
    var ajaxurl = objs_booking_list.plugin_path;
    var booking_id = jQuery(this).attr('data-id');
    var id = '1'; //jQuery(this).attr('data-id');
    var name = 'Bedroom'; //jQuery(this).attr('data-name');

    jQuery(".loader").show();
    var postdata = {
        id: id,
        name: name,
        booking_id: booking_id,
        action: 'get_article_type'
    };
    jQuery.ajax({
        type: "POST",
        url: ajaxurl + "/assets/lib/mp_front_dashboad_ajax.php",
        dataType: 'html',
        data: postdata,
        success: function(response) {
            jQuery('.articles_tab').html(response);
            var check_li = true;

            jQuery(".cart_li .room_type").each(function() {
                var li_id = jQuery(this).attr("data-li_id");

                var ul_check_id = "articles_ul_" + li_id;
                if (jQuery("." + ul_check_id + " li").length == 0) {
                    jQuery('.cart_check_' + li_id).remove();
                }
                if (id == li_id) {
                    check_li = false;
                }
            });
            jQuery(".loader").hide();

        }

    });
});

/**GET ARTICALES AND ROOM TYPE DATA **/

jQuery(document).on('click', '.room_type_cat', function() {
    console.log('call category');
    jQuery(document).ready(() => {
        jQuery("#amt_val").val('Select Insurance');
    });
    var ajaxurl = objs_booking_list.plugin_path;
    var id = jQuery(this).attr('data-id');
    var name = jQuery(this).attr('data-name');
    var booking_id = jQuery('.booking_id').val();

    jQuery(".loader").show();
    var postdata = {
        id: id,
        name: name,
        booking_id: booking_id,
        action: 'get_article_type'
    };
    jQuery.ajax({
        type: "POST",
        url: ajaxurl + "/assets/lib/mp_front_dashboad_ajax.php",
        dataType: 'html',
        data: postdata,
        success: function(response) {
            jQuery('.articles_tab').html(response);
            var check_li = true;

            jQuery(".cart_li .room_type").each(function() {
                var li_id = jQuery(this).attr("data-li_id");

                var ul_check_id = "articles_ul_" + li_id;
                if (jQuery("." + ul_check_id + " li").length == 0) {
                    jQuery('.cart_check_' + li_id).remove();
                }
                if (id == li_id) {
                    check_li = false;
                }
            });
            jQuery(".loader").hide();

        }

    });

});


/** MINUS BUTTON FUNCTION FOR STEP TWO **/

jQuery(document).on('click', '.minus', function(event) {
    jQuery(document).ready(() => {
        jQuery("#amt_val").val('Select Insurance');
    });
    var ajaxurl = objs_booking_list.plugin_path;
    var $input = jQuery(this).parent().find('input');
    var currency_symbol = jQuery('#dollar').val();
    var count = parseInt($input.val()) - 1;
    if (count > -1) {
        count = count < 1 ? 0 : count;
        $input.val(count);
        $input.change();

        var moveto_cubic_meter = moveto_cubic_unit;
        if (moveto_cubic_meter == 'm_3') {
            var label_cubic_unit = 'Cubic Meter';
        } else {
            var label_cubic_unit = 'Cubic Feet';
        }

        var get_article_room_id = jQuery(this).attr('data-room_id');
        var get_article_name = jQuery(this).attr('data-name');
        var get_article_id = jQuery(this).attr('data-id');
        var get_category_id = jQuery(this).attr('data-catid');
        var get_article_price = jQuery(this).attr('data-price');
        var get_article_caltype = jQuery(this).attr('data-caltype');
        var get_article_cubic = jQuery(this).attr('data-cubic');
        var cubic_count = '0';
        jQuery('.loader').show();
        jQuery('.get_' + get_article_id + '' + get_category_id + '' + get_article_room_id).prop('checked', true);
        var check_li = true;
        jQuery(".articles_ul_" + get_article_room_id).find('li').each(function() {
            var li_id = jQuery(this).attr("data-li_id");
            if (get_article_id + "" + get_category_id == li_id) {
                check_li = false;
            }
        });

        if (count == 0) {
            jQuery('.get_' + get_article_id + '' + get_category_id + '' + get_article_room_id).prop('checked', false);
            jQuery(".cart_check_" + get_article_id + get_article_room_id + get_category_id).html();

            jQuery(".cart_check_" + get_article_id + get_article_room_id + get_category_id).remove();
            var counts = 0;
            jQuery(".num_" + get_article_room_id + get_article_id + get_category_id).val(counts);

            var li_id = jQuery(this).attr("data-li_id");

            jQuery(".cart-value_" + get_article_id + get_article_room_id + get_category_id).html();
            jQuery(".title_" + get_article_id + get_article_room_id).html();
        }

        if (check_li) {

            var return_string = '<li class="cart-listing cart_check_' + get_article_id + '' + get_article_room_id + '' + get_category_id + '" data-li_id="' + get_article_id + '">' +
                '<h3 class="cart-title">' + get_article_name + '</h3>' +
                '<h4 class="cart-value_' + get_article_id + '' + get_article_room_id + '' + get_category_id + '">' + count + '</h4>' +
                //'<h4 class="cart-total_'+get_article_id+''+get_article_room_id+'">'+get_article_price+'</h4>'+
                '</li>';
            jQuery(".articles_ul_" + get_article_room_id).append(return_string);

        } else {

            var return_string = '<h4 class="cart-value_' + get_article_id + '' + get_article_room_id + '' + get_category_id + '">' + count + '</h4></li>';
            jQuery(".cart-value_" + get_article_id + get_article_room_id + get_category_id).html(return_string);
            var return_total = '<h4 class="cart-total_' + get_article_id + '' + get_article_room_id + '">' + currency_symbol + count * get_article_price + '</h4>' +
                jQuery(".cart-cost").html(return_total);
        }
        var cart_data_send = {
            get_article_room_id: get_article_room_id,
            get_article_id: get_article_id,
            get_article_name: get_article_name,
            count: count,
            cubic_count: cubic_count,
            get_article_cubic: get_article_cubic,
            get_article_price: get_article_price,
            get_article_cat_id: get_category_id,
            get_article_caltype: get_article_caltype,
            action: 'cart_insert_data'
        };


        jQuery.ajax({
            type: "POST",
            url: ajaxurl + "/assets/lib/mp_front_dashboad_ajax.php",
            dataType: 'html',
            data: cart_data_send,
            success: function(response) {
                jQuery('.loader').hide();
                var res_split = response.split("-");
                jQuery('.lod_esti_title').html("Loading Estimation");
                jQuery('.load_floor').html("Floor Price");
                jQuery('.third_step_val_div').html(res_split[0]);
                jQuery('.unlod_esti_title').html("Unloading Estimation");
                jQuery('.unload_floor').html("Floor Price");
                jQuery('.third_step_val_div_unload').html(res_split[1]);
                jQuery('.elevator').html("Elevator Price");
                jQuery('.third_step_val_div_elevators').html("-" + res_split[2]);
                jQuery('.unelevator').html("Elevator Price");
                jQuery('.unload_ele').html("-" + res_split[3]);
                jQuery('.packing').html("Packaging Price");
                jQuery('.third_step_val_div_packing').html(res_split[4]);
                jQuery('.unpacking').html("Unpackaging Price");
                jQuery('.third_step_val_div_unpacking').html(res_split[5]);
                jQuery('.discount_label').html("Discount");
                jQuery('.discounted_amount').html("-" + res_split[6]);
                jQuery('.tax_label').html("GST/TAX");
                jQuery('.taxed_amount').html(res_split[7]);
                jQuery('.est_cost').html("Total");
                jQuery('.total_cost').html(res_split[8]);
                jQuery('.subtotal_label').html("Subtotal");
                jQuery('.subtotal_amount').html(res_split[9]);
                jQuery('.cart_title_cubic').html("Total Volume(" + label_cubic_unit + ")");
                jQuery('.cart_cubic_value').html(res_split[10]);
                jQuery('.cart_cubic_volume').html(res_split[11]);
                //jQuery('.insurance_rate').html(res_split[12]);
                //jQuery('.insurance_rate').hide();
                jQuery('.insurance_data').remove();

            }
        });

    }
    var article_div = jQuery(".articles_ul_" + get_article_room_id).text()
    if (article_div == '') {
        jQuery(".cart_check_" + get_article_room_id).remove();
    }

    var summaryData = jQuery(".cart_li").text();
    if (jQuery.trim(summaryData) == '') {
        jQuery(".r-cart-a").hide();
    }
});

/** PLUS BUTTON FUNCTION FOR STEP TWO **/

jQuery(document).on('click', '.plus', function(event) {

    jQuery(document).ready(() => {
        jQuery("#amt_val").val('Select Insurance');
    });
    var max_qty = parseInt(jQuery(this).attr("data-max_qty")) + 1;
    var ajaxurl = objs_booking_list.plugin_path;
    var $input = jQuery(this).parent().find('input');
    var currency_symbol = jQuery('#dollar').val();
    var moveto_cubic_meter = moveto_cubic_unit;
    if (moveto_cubic_meter == 'm_3') {
        var label_cubic_unit = 'Cubic Meter';
    } else {
        var label_cubic_unit = 'Cubic Feet';
    }

    var count = parseInt($input.val()) + 1;
    if (count == max_qty) {
        return false;
    }
    $input.val(count);
    $input.change();

    var get_article_room_id = jQuery(this).attr('data-room_id');
    var get_article_name = jQuery(this).attr('data-name');
    var get_article_id = jQuery(this).attr('data-id');
    var get_category_id = jQuery(this).attr('data-catid');
    var get_article_price = jQuery(this).attr('data-price');
    var get_article_cubic = jQuery(this).attr('data-cubic');
    var cubic_count = '1';

    var room_name = jQuery(this).attr('data-room');
    var get_article_caltype = jQuery(this).attr('data-caltype');
    jQuery('.loader').show();

    if (jQuery('.cart_check_' + get_article_room_id).length == 0) {
        var return_string = '<li class="cart-listing room_type cart_check_' + get_article_room_id + '" data-li_id="' + get_article_room_id + '">' +
            '<h3 class="cart-title cart_title">' + room_name + '</h3>' +
            '<ul class="articles_ul_' + get_article_room_id + '"></ul>' +
            '</li>';
        jQuery(".cart_li").append(return_string);
    }

    jQuery('.get_' + get_article_id + '' + get_category_id + '' + get_article_room_id).prop('checked', true);
    var check_li = true;
    jQuery(".articles_ul_" + get_article_room_id).find('li').each(function() {
        var li_id = jQuery(this).attr("data-li_id");
        if (get_article_id + "" + get_category_id == li_id) {
            check_li = false;
        }
    });
    if (check_li) {

        var return_string = '<li class="cart-listing cart_check_' + get_article_id + '' + get_article_room_id + '' + get_category_id + '" data-li_id="' + get_article_id + '' + get_category_id + '">' +
            '<h3 class="cart-title title_' + get_article_id + '' + get_article_room_id + '' + get_category_id + '">' + get_article_name + '</h3>' +
            '<h4 class="cart-value_' + get_article_id + '' + get_article_room_id + '' + get_category_id + '">' + count + '</h4>' +
            /*'<h4 class="cart-total_'+get_article_id+''+get_article_room_id+'">'+get_article_price+'</h4>'+*/
            '</li>';
        jQuery(".articles_ul_" + get_article_room_id).append(return_string);

        jQuery('.r-cart-a').show();
    } else {

        var return_string = '<h4 class="cart-value_' + get_article_id + '' + get_article_room_id + '' + get_category_id + '">' + count + '</h4></li>';
        /*var return_total =	'<h4 class="cart-total_'+get_article_id+''+get_article_room_id+'">'+'$'+count*get_article_price+'</h4>'+*/
        jQuery(".cart-value_" + get_article_id + get_article_room_id + get_category_id).html(return_string);

    }
    var cart_data_send = {
        get_article_room_id: get_article_room_id,
        get_article_id: get_article_id,
        get_article_name: get_article_name,
        count: count,
        cubic_count: cubic_count,
        get_article_cubic: get_article_cubic,
        get_article_price: get_article_price,
        get_article_cat_id: get_category_id,
        get_article_caltype: get_article_caltype,
        action: 'cart_insert_data'
    };

    jQuery.ajax({
        type: "POST",
        url: ajaxurl + "/assets/lib/mp_front_dashboad_ajax.php",
        dataType: 'html',
        data: cart_data_send,
        success: function(response) {
            jQuery('.loader').hide();
            var res_split = response.split("-");
            jQuery('.lod_esti_title').html("Loading Estimation");
            jQuery('.load_floor').html("Floor Price");
            jQuery('.third_step_val_div').html(res_split[0]);
            jQuery('.unlod_esti_title').html("Unloading Estimation");
            jQuery('.unload_floor').html("Floor Price");
            jQuery('.third_step_val_div_unload').html(res_split[1]);
            jQuery('.elevator').html("Elevator Price");
            jQuery('.third_step_val_div_elevators').html("-" + res_split[2]);
            jQuery('.unelevator').html("Elevator Price");
            jQuery('.unload_ele').html("-" + res_split[3]);
            jQuery('.packing').html("Packaging Price");
            jQuery('.third_step_val_div_packing').html(res_split[4]);
            jQuery('.unpacking').html("Unpackaging Price");
            jQuery('.third_step_val_div_unpacking').html(res_split[5]);
            jQuery('.discount_label').html("Discount");
            jQuery('.discounted_amount').html("-" + res_split[6]);
            jQuery('.tax_label').html("GST/TAX");
            jQuery('.taxed_amount').html(res_split[7]);
            jQuery('.est_cost').html("Total");
            jQuery('.total_cost').html(res_split[8]);
            jQuery('.subtotal_label').html("Subtotal");
            jQuery('.subtotal_amount').html(res_split[9]);
            jQuery('.cubic_feets').html(label_cubic_unit + " Rate");
            jQuery('.cart_title_cubic').html("Total Volume(" + label_cubic_unit + ")");
            jQuery('.cart_cubic_value').html(res_split[10]);
            jQuery('.cart_cubic_volume').html(res_split[11]);
            //jQuery('.insurance_rate').html(res_split[12]);
            //jQuery('.insurance_rate').hide();
            jQuery('.insurance_data').remove();
        }
    });
});


/** GET ARTICALES DATA FOR STEP TWO **/

jQuery(document).on('click', '.get_article', function(event) {
    jQuery(document).ready(() => {
        jQuery("#amt_val").val('Select Insurance');
    });
    var get_article_room_id = jQuery(this).attr('data-room_id');
    var get_article_id = jQuery(this).attr('data-id');
    var get_category_id = jQuery(this).attr('data-catid');
    var get_article_name = jQuery(this).attr('data-name');
    var room_name = jQuery(this).attr('data-room');
    var get_article_cubic = jQuery(this).attr('data-cubic');

    var moveto_cubic_meter = moveto_cubic_unit;
    if (moveto_cubic_meter == 'm_3') {
        var label_cubic_unit = 'Cubic Meter';
    } else {
        var label_cubic_unit = 'Cubic Feet';
    }
    if (jQuery('.get_' + get_article_id + '' + get_category_id + '' + get_article_room_id).prop("checked") == true) {
        if (jQuery('.cart_check_' + get_article_room_id).length == 0) {

            var return_string = '<li class="cart-listing room_type cart_check_' + get_article_room_id + '" data-li_id="' + get_article_room_id + '">' +
                '<h3 class="cart-title cart_title">' + room_name + '</h3>' +
                '<ul class="articles_ul_' + get_article_room_id + '"></ul>' +
                '</li>';
            jQuery(".cart_li").append(return_string);
        }
        var counts = 1;
        jQuery("#plus" + get_article_room_id + get_article_id + get_category_id).trigger("click");
        jQuery(".num_" + get_article_room_id + get_article_id + get_category_id).val(counts);
        jQuery('.r-cart-a').show();
    } else if (jQuery('.get_' + get_article_id + '' + get_category_id + '' + get_article_room_id).prop("checked") == false) {
        var cubic_count = 0;
        var counts = 0;
        jQuery(".num_" + get_article_room_id + get_article_id + get_category_id).val(counts);


        var ajaxurl = objs_booking_list.plugin_path;
        currency_symbol = jQuery('#dollar').val();
        var count = 0;
        if (count > -1) {
            count = count < 1 ? 0 : count;

            var get_article_price = jQuery(this).attr('data-price');
            var get_article_caltype = jQuery(this).attr('data-caltype');

            jQuery('.get_' + get_article_id + '' + get_category_id + '' + get_article_room_id).prop('checked', true);
            var check_li = true;
            jQuery(".articles_ul_" + get_article_room_id).find('li').each(function() {
                var li_id = jQuery(this).attr("data-li_id");
                if (get_article_id + "" + get_category_id == li_id) {
                    check_li = false;
                }
            });

            if (count == 0) {
                jQuery('.get_' + get_article_id + '' + get_category_id + '' + get_article_room_id).prop('checked', false);
                jQuery(".cart_check_" + get_article_id + get_article_room_id + get_category_id).html();

                jQuery(".cart_check_" + get_article_id + get_article_room_id + get_category_id).remove();
                var counts = 0;
                jQuery(".num_" + get_article_room_id + get_article_id + get_category_id).val(counts);

                var li_id = jQuery(this).attr("data-li_id");

                jQuery(".cart-value_" + get_article_id + get_article_room_id + get_category_id).html();
                jQuery(".title_" + get_article_id + get_article_room_id).html();

            }
            if (check_li) {

                var return_string = '<li class="cart-listing cart_check_' + get_article_id + '' + get_article_room_id + '' + get_category_id + '" data-li_id="' + get_article_id + '">' +
                    '<h3 class="cart-title">' + get_article_name + '</h3>' +
                    '<h4 class="cart-value_' + get_article_id + '' + get_article_room_id + '' + get_category_id + '">' + count + '</h4>' +
                    /*'<h4 class="cart-total_'+get_article_id+''+get_article_room_id+'">'+get_article_price+'</h4>'+*/
                    '</li>';
                jQuery(".articles_ul_" + get_article_room_id).append(return_string);

            } else {

                var return_string = '<h4 class="cart-value_' + get_article_id + '' + get_article_room_id + '' + get_category_id + '">' + count + '</h4></li>';
                jQuery(".cart-value_" + get_article_id + get_article_room_id + get_category_id).html(return_string);
                var return_total = '<h4 class="cart-total_' + get_article_id + '' + get_article_room_id + '">' + currency_symbol + count * get_article_price + '</h4>' +
                    jQuery(".cart-cost").html(return_total);
            }
            var cart_data_send = {
                get_article_room_id: get_article_room_id,
                get_article_id: get_article_id,
                get_article_name: get_article_name,
                count: count,
                cubic_count: cubic_count,
                get_article_cubic: get_article_cubic,
                get_article_price: get_article_price,
                get_article_cat_id: get_category_id,
                get_article_caltype: get_article_caltype,
                action: 'cart_insert_data'
            };


            jQuery.ajax({
                type: "POST",
                url: ajaxurl + "/assets/lib/mp_front_dashboad_ajax.php",
                dataType: 'html',
                data: cart_data_send,
                success: function(response) {
                    var res_split = response.split("-");
                    jQuery('.lod_esti_title').html("Loading Estimation");
                    jQuery('.load_floor').html("Floor Price");
                    jQuery('.third_step_val_div').html(res_split[0]);
                    jQuery('.unlod_esti_title').html("Unloading Estimation");
                    jQuery('.unload_floor').html("Floor Price");
                    jQuery('.third_step_val_div_unload').html(res_split[1]);
                    jQuery('.elevator').html("Elevator Price");
                    jQuery('.third_step_val_div_elevators').html("-" + res_split[2]);
                    jQuery('.unelevator').html("Elevator Price");
                    jQuery('.unload_ele').html("-" + res_split[3]);
                    jQuery('.packing').html("Packaging Price");
                    jQuery('.third_step_val_div_packing').html(res_split[4]);
                    jQuery('.unpacking').html("Unpackaging Price");
                    jQuery('.third_step_val_div_unpacking').html(res_split[5]);
                    jQuery('.discount_label').html("Discount");
                    jQuery('.discounted_amount').html("-" + res_split[6]);
                    jQuery('.tax_label').html("GST/TAX");
                    jQuery('.taxed_amount').html(res_split[7]);
                    jQuery('.est_cost').html("Total");
                    jQuery('.total_cost').html(res_split[8]);
                    jQuery('.subtotal_label').html("Subtotal");
                    jQuery('.subtotal_amount').html(res_split[9]);
                    jQuery('.cart_title_cubic').html("Total Volume(" + label_cubic_unit + ")");
                    jQuery('.cart_cubic_value').html(res_split[10]);
                    jQuery('.cart_cubic_volume').html(res_split[11]);
                    jQuery('.insurance_data').remove();
                    //jQuery('.insurance_rate').html(res_split[12]);
                    //jQuery('.insurance_rate').hide();
                    //jQuery('.insurance_data').remove();
                    /* jQuery('.amt_val').append('<div class="mp-xs-12 text-center cart-bg custom_insurance"> '
                    						+'<div class="mp-xs-5">'+
                    							'<label class="">Insurance Amount</label>'+
                    						'</div>'+
                    						'<div class="mp-xs-7">'+
                    							'<select name="amt_val" id="amt_val">'+
                    								'<option>Select Insurance</option>'+
                    							    '<option value="50000">50000</option>'+
                    							    '<option value="100000">100000</option>'+
                    							    '<option value="150000">150000</option>'+
                    							    '<option value="200000">200000</option>'+
                    							    '<option value="other">Other</option>'+
                    							'</select>'+
                    						'</div>'
                    						
                    					); */
                }
            });

        }
        var article_div1 = jQuery(".articles_ul_" + get_article_room_id).text()
        if (article_div1 == '') {
            jQuery(".cart_check_" + get_article_room_id).remove();
        }
        var summaryData = jQuery(".cart_li").text();
        if (jQuery.trim(summaryData) == '') {

            jQuery(".r-cart-a").hide();
        }
    }
});

/** booking update **/
jQuery(document).on('click', '.booking_update', function(event) {
    var ajaxurl = objs_booking_list.plugin_path;
    var booking_id = jQuery(this).data("id");
    if (booking_id) {
        var current_stepdata = { booking_id: booking_id, action: 'booking_data_update' }

        jQuery.ajax({
            type: "POST",
            url: ajaxurl + "/assets/lib/mp_front_dashboad_ajax.php",
            dataType: 'html',
            data: current_stepdata,
            success: function(response) {
                if (response == 1) {
                    /*alert("Booking update successfully");*/
                    location.reload();
                } else {
                    alert("Somthing want'to wrong..");
                }
            }
        });
    } else {
        alert("Somthing want'to wrong..");
    }
});
/** booking update over **/